package com.elsewhat.reddittv.hd;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebSettings.PluginState;
import android.widget.Toast;

public class YoutubePlayerActivty extends Activity {
	private WebView webView;
	private Context context;
	public YoutubePlayerActivty (){
		
	}


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context= this;
		webView = new WebView (context);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setPluginState(PluginState.ON);
		webView.setWebChromeClient(new SimpleWebChromeClient());
		
		//String youtubeId = redditPost.getYoutubeId();
		//webView.loadUrl("http://www.youtube.com/embed/"+youtubeId+"?autoplay=1&enablejsapi=1&playerapiid=ytplayer&hd=1");
		String html = getHTML();
		//register a javascript interface with the webview for listening to events from the youtube player
		webView.addJavascriptInterface(new RedditTVHDJavascriptInterface(context), "Android");
		//load the youtube player for this reddit post
		webView.loadDataWithBaseURL("http://www.youtube.com", html, "text/html", "utf-8", null);
		Log.d(RedditTVHDActivity.LOG_PREFIX, html);
		//webView.loadData(html, "text/html", "utf-8");
		//webView.loadUrl("http://dl.dropbox.com/u/4379928/yt_test.html");
		
		
		setContentView(webView);
	}
	
	public String getHTML(){
		String html= "<script src=\"http://www.youtube.com/player_api\"></script>\n"+
			"<script type=\"text/javascript\">\n"+
			"var player;\n"+
	"\n"+
			"function onPlayerReady(event) {\n"+
			"	Android.onPlayerReady('onPlayerReady');\n"+
			"	//event.target.playVideo();\n"+
			"}\n"+
	"\n"+
			"function onPlayerStateChange(event) {\n"+
			"	Android.onPlayerReady('onPlayerStateChange');\n"+
			"}\n"+
			"//setup player and event listeners\n"+
			"function onYouTubePlayerAPIReady() {\n"+
			"	//alert('In onYouTubePlayerAPIReady');\n"+
			"	player = new YT.Player('ytplayer', {\n"+
			"		events: {\n"+
			"			'onReady': onPlayerReady,\n"+
			"			'onStateChange': onPlayerStateChange\n"+
			"		}\n"+
			"	});\n"+
			"}\n"+
	"\n"+
			"</script>\n"+
	"\n"+
			"<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 100%\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"+
			"9OrxqVWTpIU"+"?enablejsapi=1&hd=1\" frameborder=\"0\">\n"+
			"</iframe>\n";
		return html;
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Fragment#onDestroy()
	 */
	@Override
	public void onDestroy() {
		Log.w(RedditTVHDActivity.LOG_PREFIX, "In onDestroy");
		super.onDestroy();
	}
	/* (non-Javadoc)
	 * @see android.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		Log.w(RedditTVHDActivity.LOG_PREFIX, "In onPause");
		super.onPause();
	}
	/* (non-Javadoc)
	 * @see android.app.Fragment#onStop()
	 */
	@Override
	public void onStop() {
		Log.w(RedditTVHDActivity.LOG_PREFIX, "In onStop");
		if(webView!=null){
			webView.destroy();
		}
		
		super.onStop();
	}

	
	public class RedditTVHDJavascriptInterface {
	    Context mContext;

	    /** Instantiate the interface and set the context */
	    RedditTVHDJavascriptInterface(Context c) {
	        mContext = c;
	    }

	    /** Show a toast from the web page */
	    public void onPlayerReady(String toast) {
	        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
	    }
	    /** Show a toast from the web page */
	    public void onPlayerStateChange(String toast) {
	        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
	    }
	}
	
	private class SimpleWebChromeClient extends WebChromeClient {

		/* (non-Javadoc)
		 * @see android.webkit.WebChromeClient#onShowCustomView(android.view.View, android.webkit.WebChromeClient.CustomViewCallback)
		 */
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			super.onShowCustomView(view, callback);
			Log.w(RedditTVHDActivity.LOG_PREFIX, "In OnShowCustomView");
		}
		
	}

}
